package com.jxm.hutools;

import cn.hutool.core.date.*;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2023-12-18 14:39
 * @Version: 1.0
 */
public class DateUtilTest {

    //Date、long、Calendar之间的相互转换
    @Test
    public void test1(){
        //当前时间
        Date date = DateUtil.date();
        System.out.println(date);
        //当前时间
        Date date2 = DateUtil.date(Calendar.getInstance());
        System.out.println(date2);
        //当前时间
        Date date3 = DateUtil.date(System.currentTimeMillis());
        System.out.println(date3);
        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        String now = DateUtil.now();
        System.out.println(now);
        //当前日期字符串，格式：yyyy-MM-dd
        String today= DateUtil.today();
        System.out.println(today);
    }

    //字符串转日期
    @Test
    public void test2(){
        String dateStr = "2017-03-01 15:33:22";
        Date date = DateUtil.parse(dateStr);
        System.out.println(date);

        String dateStr2 = "2017-03-01";
        Date date2 = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss");
        System.out.println(date2);
    }

    //格式化日期输出
    @Test
    public void test3(){
        String dateStr = "2017-03-01";
        Date date = DateUtil.parse(dateStr);

        //结果 2017/03/01
        String format = DateUtil.format(date, "yyyy/MM/dd");
        System.out.println(format);
        //常用格式的格式化，结果：2017-03-01
        String formatDate = DateUtil.formatDate(date);
        System.out.println(formatDate);
        //结果：2017-03-01 00:00:00
        String formatDateTime = DateUtil.formatDateTime(date);
        System.out.println(formatDateTime);
        //结果：00:00:00
        String formatTime = DateUtil.formatTime(date);
        System.out.println(formatTime);
    }

    //获取Date对象的某个部分
    @Test
    public void test4(){
        Date date = DateUtil.date();
        System.out.println(date);
        int year = DateUtil.year(date);
        System.out.println(year);
        int month = DateUtil.month(date);
        System.out.println(month);
        //获得月份枚举
        Month month1 = DateUtil.monthEnum(date);
        System.out.println(month1.getValue());

    }

    //开始和结束时间
    @Test
    public void test5(){
        String dateStr = "2017-03-01 22:33:23";
        Date date = DateUtil.parse(dateStr);

        //一天的开始，结果：2017-03-01 00:00:00
        Date beginOfDay = DateUtil.beginOfDay(date);
        System.out.println(beginOfDay);

        //一天的结束，结果：2017-03-01 23:59:59
        Date endOfDay = DateUtil.endOfDay(date);
        System.out.println(endOfDay);
    }

    //日期时间偏移
    @Test
    public void test6(){
        String dateStr = "2017-03-01 22:33:23";
        Date date = DateUtil.parse(dateStr);
        System.out.println(date);
        //结果：2017-03-03 22:33:23
        Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
        System.out.println(newDate);

        //常用偏移，结果：2017-03-04 22:33:23
        DateTime newDate2 = DateUtil.offsetDay(date, 3);
        System.out.println(newDate2);

        //常用偏移，结果：2017-03-01 19:33:23
        DateTime newDate3 = DateUtil.offsetHour(date, -3);
        System.out.println(newDate3);

        DateTime dateTime = DateUtil.offsetMonth(date, 3);
        System.out.println(dateTime);

        DateTime dateTime1 = DateUtil.offsetMonth(date, -12);
        System.out.println(dateTime1);
    }

    //针对当前时间，提供了简化的偏移方法（例如昨天、上周、上个月等）：
    @Test
    public void test7(){
        //昨天
        System.out.println(DateUtil.yesterday());
        //明天
        System.out.println(DateUtil.tomorrow());
        //上周
        System.out.println(DateUtil.lastWeek());
        //下周
        System.out.println(DateUtil.nextWeek());
        //上个月
        System.out.println(DateUtil.lastMonth());
        //下个月
        System.out.println(DateUtil.nextMonth());
    }


    //日期时间差
    @Test
    public void test8(){
        String dateStr1 = "2023-10-01 22:33:23";
        Date date1 = DateUtil.parse(dateStr1);

        String dateStr2 = "2023-11-01 22:33:23";
        Date date2 = DateUtil.parse(dateStr2);
//        date2 = DateUtil.date();

        //相差天数
        long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
        System.out.println(betweenDay);

        //相差天数
        System.out.println(DateUtil.between(date1, date2, DateUnit.HOUR));
        LocalDateTime localDateTime = DateUtil.toLocalDateTime(date2);
        LocalDateTime localDateTime1 = LocalDateTimeUtil.now();
        System.out.println(LocalDateTimeUtil.between(localDateTime, localDateTime1).toMillis());

        System.out.println(date1.getTime());
    }

    @Test
    public void test9(){
        // "摩羯座"
        String zodiac = DateUtil.getZodiac(Month.JANUARY.getValue(), 19);
        System.out.println(zodiac);

        // "狗"
        String chineseZodiac = DateUtil.getChineseZodiac(1994);
        System.out.println(chineseZodiac);

        //年龄
        System.out.println(DateUtil.ageOfNow("1993-10-12"));


        //是否闰年
        System.out.println(DateUtil.isLeapYear(2020));
    }


}
